package org.xg.study.pd.test;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xg.study.pd.Admin;
import org.xg.study.pd.ConfigFile;
import org.xg.study.pd.ConfigOriginator;

import java.util.Date;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName ApiTest17_00
 * @date 2021/9/17 9:31
 **/
public class ApiTest17_00 {
    private Logger logger = LoggerFactory.getLogger(ApiTest17_00.class);

    @Test
    public void test(){
        Admin admin = new Admin();
        ConfigOriginator configOriginator = new ConfigOriginator();
        configOriginator.setConfigFile(new ConfigFile("100000001","配置内容A=哈哈",new Date(),"xg"));
        admin.append(configOriginator.saveMemento());//保存配置
        configOriginator.setConfigFile(new ConfigFile("100000002","配置内容A=嘿嘿",new Date(),"xg"));
        admin.append(configOriginator.saveMemento());//保存配置
        configOriginator.setConfigFile(new ConfigFile("100000003","配置内容A=么么",new Date(),"xg"));
        admin.append(configOriginator.saveMemento());//保存配置
        configOriginator.setConfigFile(new ConfigFile("100000004","配置内容A=嘻嘻",new Date(),"xg"));
        admin.append(configOriginator.saveMemento());//保存配置

        //历史配置回滚
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置（回滚） undo：{}", JSONObject.toJSONString(configOriginator.getConfigFile()));
        logger.info("================================分割线=======================================");

        //历史配置回滚
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置（回滚） undo：{}", JSONObject.toJSONString(configOriginator.getConfigFile()));
        logger.info("================================分割线=======================================");

        //历史配置前进
        configOriginator.getMemento(admin.redo());
        logger.info("历史配置（前进） redo：{}", JSONObject.toJSONString(configOriginator.getConfigFile()));
        logger.info("================================分割线=======================================");

        //历史配置获取
        configOriginator.getMemento(admin.get("100000002"));
        logger.info("历史配置（获取） get：{}", JSONObject.toJSONString(configOriginator.getConfigFile()));
        logger.info("================================分割线=======================================");
    }
}
